package com.jiangxjsy.core.wrap;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/5/28 20:03
 */

public interface ResultCode {
    /**
     * 获取状态码
     *
     * @return 状态码
     */
    int getCode();

    /**
     * 获取状态码信息
     *
     * @return 状态码信息
     */
    String getMsg();


    /**
     * 根据状态码获取任意一个实现了 ResultCode 的枚举对象
     *
     * @param code 状态码
     * @return 实现了 ResultCode 的枚举对象
     */
    static ResultCode parse(Integer code) {
        for (SystemResultCode resultCode : SystemResultCode.values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }

        for (AuthorizationResultCode resultCode : AuthorizationResultCode.values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }

        for (AdminResultCode resultCode : AdminResultCode.values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }

        return SystemResultCode.SYSTEM_ERROR;
    }

    /**
     * 根据状态码获取 SystemResultCode 枚举
     *
     * @param code 状态码
     * @param clazz 枚举类 SystemResultCode 类对象
     * @return 枚举
     */
    static SystemResultCode parse(Integer code, Class<SystemResultCode> clazz) {
        for (SystemResultCode resultCode : clazz.getEnumConstants()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }
        return SystemResultCode.SYSTEM_ERROR;
    }


    enum SystemResultCode implements ResultCode {
        SYSTEM_OK(0, "success"),
        SYSTEM_ERROR(500, "network error, please try again later"),
        SYSTEM_NOT_LOGIN(302, "please log in first!"),
        SYSTEM_NO_VALID(400, "parameter validation error"),
        SYSTEM_NO_AUTH(401, "unauthorized operation"),
        SYSTEM_NO_FOUND(404, "resource not found"),
        SYSTEM_NO_SUCH_PARAMETER_ERROR(1003, "parameter does not exist!");

        private final int code;
        private final String msg;

        SystemResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        /**
         * 获取状态码
         *
         * @return 状态码
         */
        @Override
        public int getCode() {
            return code;
        }

        /**
         * 获取状态码信息
         *
         * @return 状态码信息
         */
        @Override
        public String getMsg() {
            return msg;
        }
    }


    enum AuthorizationResultCode implements ResultCode {
        AUTHORIZATION_USERNAME_REQUIRED(1002, "please enter your username"),
        AUTHORIZATION_PASSWORD_REQUIRED(1003, "please enter your password or SMS verification code"),
        AUTHORIZATION_LOGIN_NO_USER(1004, "account or password error"),
        AUTHORIZATION_LOGIN_ERR(1005, "login authentication failed"),
        AUTHORIZATION_USER_DISABLE_ERROR(1007, "account disabled"),
        AUTHORIZATION_USER_DOES_NOT_EXIST(1009, "the user does not exist, please register first"),
        AUTHORIZATION_LOGIN_PASSWORD_TO_MANY_ERROR(1010, "too many password errors, please try again after %s");

        private final int code;
        private final String msg;

        AuthorizationResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        /**
         * 获取状态码
         *
         * @return 状态码
         */
        @Override
        public int getCode() {
            return code;
        }

        /**
         * 获取状态码信息
         *
         * @return 状态码信息
         */
        @Override
        public String getMsg() {
            return msg;
        }
    }


    enum AdminResultCode implements ResultCode {
        ADMIN_PASSWORD_INTENSITY_ERROR(1106, "the password must consist of 6-20 letters and numbers"),
        ADMIN_USER_EXIST_ERROR(1107, "the user already exists"),
        ADMIN_USER_NOT_ROLE_ERROR(1113, "please set roles for users first"),
        ADMIN_USER_NOT_DEPT_ERROR(1114, "please set up a department for the user first"),
        ADMIN_SUPER_USER_DISABLED_ERROR(1116, "super administrator user cannot be disabled"),
        ADMIN_USER_REAL_NAME_EXIST_ERROR(1141, "duplicate user name");

        private final int code;
        private final String msg;

        AdminResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        /**
         * 获取状态码
         *
         * @return 状态码
         */
        @Override
        public int getCode() {
            return code;
        }

        /**
         * 获取状态码信息
         *
         * @return 状态码信息
         */
        @Override
        public String getMsg() {
            return msg;
        }
    }
}
